<?php
namespace Tbf\Util;
use tbf\error;
/**
 * php全局请求
 func method()   http方法
 func data()     请求
 func header()   请求头信息

 func url()      完整url
 func urlEnter() 获取apache2服务器里面，本入口文件相对于url所在的位置(有域名)
 func urlPathEnter() url路径里面的相对入口（没域名）
 func urlRefPath()  相对于本应用的路径（没域名）
 func urlPath()  url里面的完整路径（没域名）
 func segment($num=null)  本应用路径数组形式。（传入数字直接返回第num个segment，从0开始）
 func isCmd()    是不是cmd模式
 例子：
 apache2的配置
 ServerName www.example.com
 DocumentRoot /var/example

 入口文件位置 /var/example/tbf/index.php

 请求url GET http://www.example.com/tbf/admin/123/?a=1
 则：
 url      http://www.example.com/tbf/admin/123/?a=1
 method   GET
 data     array('a'=>'1');
 urlEnter http://www.example.com/tbf/
 urlPathEnter /tbf
 urlPath  /tbf/admin/123
 urlRefPath  /admin/123
 segment  ['admin','123']
 isCmd    false
 */
/**
 * @deprecated
 */
class globalReq
{
    /**
     * http方法
     */
    public static function method()
    {
        if (isset($_SERVER['REQUEST_METHOD'])) {
            return $_SERVER['REQUEST_METHOD'];
        }

        return error::tPanic('can get request method');
    }
    /**
     * 获取请求数据
     */
    public static function data()
    {
        if (isset($_POST)&&isset($_GET)) {
            return array_merge($_GET,$_POST);
        } elseif (isset($_REQUEST)) {
            return $_REQUEST;
        } elseif (isset($_POST)) {
            return $_POST;
        } elseif (isset($_GET)) {
            return $_GET;
        } else {
            return array();
        }
    }
    /**
     * 获取全局url
     */
    public static function url()
    {
        if (isset($_SERVER['HTTP_HOST'])) {
            $host = $_SERVER['HTTP_HOST'];
        } else {
            //host can not exist, may be cmd mod
            return error::tPanic('can not get host');
        }

        if (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443') {
            $sys_protocal = 'https://';
        } else {
            $sys_protocal = 'http://';
        }

        if ($_SERVER['PHP_SELF']) {
            $php_self = $_SERVER['PHP_SELF'];
        } else {
            $php_self = $_SERVER['SCRIPT_NAME'];
        }

        if (isset($_SERVER['PATH_INFO'])) {
            $path_info =   $_SERVER['PATH_INFO'];
        } else {
            $path_info = '';
        }

        if (isset($_SERVER['REQUEST_URI'])) {
            $relate_url = $_SERVER['REQUEST_URI'];
        } else {
            if (isset($_SERVER['QUERY_STRING'])) {
                $path_info = '?'.$_SERVER ['QUERY_STRING'];
            } else {
                $path_info = $path_info;
            }
            $relate_url = $php_self.$path_info;
        }

        return $sys_protocal.$host.$relate_url;
    }
    /**
     * url的信息
     */
    public static function urlInfo()
    {
        return parse_url(self::url());
    }
    /**
     * 获取apache2服务器里面，本入口文件相对于url所在的位置
     */
    public static function urlEnter()
    {
        return error::tPanic('未实现');
    }
    public static function urlPathEnter()
    {
        $ret = self::_loadBasePathRewrite();
        if ($ret!==null) {
            return $ret;
        }
            $ret = self::_loadBasePathNoRewrite();
        if ($ret!==null) {
            return $ret;
        }

        return error::tPanic('can not get urlPathEnter');
    }
    public static function urlPath()
    {
        $urlInfo = self::urlInfo();

        return $urlInfo['path'];
    }
    public static function urlRefPath()
    {
        if (isset($_SERVER['PATH_INFO'])) {
            return $_SERVER['PATH_INFO'];
        }
        $urlPath = self::urlPath();
        $basePath = self::urlPathEnter();
        $pos = strpos($urlPath,$basePath);
        if ($pos!==false) {
            return substr($urlPath,strlen($basePath)-1);
        }
    }
    public static function segment($num=null)
    {
        $pathInfo = self::urlRefPath();
        $data = explode('/',$pathInfo);
        $output = array();
        foreach ($data as $v1) {
            if ($v1!='') {
                $output[]=$v1;
            }
        }
        if ($num!==null) {
            if (isset($output[$num])) {
                return $output[$num];
            } else {
                return '';
            }
        }

        return $output;
    }
    /**
     * 请求header
     */
    public static function header()
    {
        $header = array();
        foreach ($_SERVER as $k1=>$v1) {
            if (strpos($k1,'HTTP_')!==0) {
                continue;
            }
            $headerKey = substr($k1,5);
            $headerKey = giveName::to($headerKey,'GIVE_NAME','Give-Name');
            $header[$headerKey] = $v1;
        }

        return $header;
    }
    public static function isCmd()
    {
        if (isset($_SERVER['SHELL'])) {
            return true;
        } else {
            return false;
        }
    }
    private static function _loadScriptNameRelationDocumentRoot()
    {
        //当前脚本文件相对网站根路径在下面变量里面（apache2）
        if (isset($_SERVER['SCRIPT_NAME'])) {
            return $_SERVER['SCRIPT_NAME'];
        } else {
            error::tPanic('can not find ScriptNameRelationDocumentRoot,'.
                    'use / as default value');
        }
    }

    private static function _loadBasePathNoRewrite()
    {
        $self = self::_loadScriptNameRelationDocumentRoot();
        //非rewrite模式，可以找到自己的脚本名
        $urlPath = self::urlPath();
        $pos = strpos($urlPath,$self);
        if ($pos===0) {
            return url::addUrlDS($self);
        }

        return null;
    }

    private static function _loadBasePathRewrite()
    {
        $self = self::_loadScriptNameRelationDocumentRoot();
        //rewrite模式，只能找到自己的脚本名所在的目录
        $url = self::urlPath();
        $self = dirname($self);
        $pos = strpos($url,$self);
        if ($pos===0) {
            return url::addUrlDS($self);
        }

        return null;
    }
}
